package com.xyh.transaction.tencentCloud;

import java.io.File;
import java.io.IOException;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.*;
import com.qcloud.cos.region.Region;

public class PutObject {
    public static void main(String[] args) {
//        putObjectDemo();
        String key = "test4.jpg";
        System.out.println(key.substring(0, key.lastIndexOf(".")));
    }

    static void putObjectDemo() {
        // 初始化用户身份信息(secretId, secretKey)

        COSCredentials cred = new BasicCOSCredentials("AKIDBHICyUKzUp38zgFl8GDzbzBdzDFRWN2W","NZvCYn8KMG1d7Umqx54uY4PXTGtTqJj0");
        // 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
        ClientConfig clientConfig = new ClientConfig(new Region("ap-shanghai"));
        // 生成cos客户端
        COSClient cosClient = new COSClient(cred, clientConfig);

        String bucketName = "img-1323317179";
        String key = "test2.jpg";
        String localPath = "C:\\xiaoyh\\images\\test2.jpg";

        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, new File(localPath));
        PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
        System.out.println(putObjectResult.getRequestId());
    }

    static void getObjectDemo() throws IOException {
        // 初始化用户身份信息(secretId, secretKey)
        COSCredentials cred = new BasicCOSCredentials("AKIDBHICyUKzUp38zgFl8GDzbzBdzDFRWN2W","NZvCYn8KMG1d7Umqx54uY4PXTGtTqJj0");
        // 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
        ClientConfig clientConfig = new ClientConfig(new Region("ap-shanghai"));
        // 生成cos客户端
        COSClient cosClient = new COSClient(cred, clientConfig);
        // Bucket的命名格式为 BucketName-APPID ，此处填写的存储桶名称必须为此格式
        String bucketName = "img-1323317179";
        String key = "test2.jpg";
        // 方法1 获取下载输入流
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
        // 限流使用的单位是 bit/s, 这里设置下载带宽限制为10MB/s
        getObjectRequest.setTrafficLimit(80*1024*1024);
        COSObject cosObject = cosClient.getObject(getObjectRequest);
        COSObjectInputStream cosObjectInput = cosObject.getObjectContent();
        // 下载对象的 CRC64
        String crc64Ecma = cosObject.getObjectMetadata().getCrc64Ecma();
        // 关闭输入流
        cosObjectInput.close();
        // 方法2 下载文件到本地
        String outputFilePath = "C:\\xiaoyh\\images\\download\\";
        File downFile = new File(outputFilePath);
        getObjectRequest = new GetObjectRequest(bucketName, key);
        ObjectMetadata downObjectMeta = cosClient.getObject(getObjectRequest, downFile);
    }
}